package com.rljk.utils;

import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * xml 工具类
 * @Function 
 * @Author zhaozhenhua
 * @Date 2018年5月19日
 */
public class XMLUtil {
   /**
    * 解析xml,返回第一级元素键值对。如果第一级元素有子节点，则此节点的值是子节点的xml数据。 微信支付 解析xml xml转map  获取prepay_id
    * @param strxml
    * @throws JDOMException
    * @throws IOException
    */
   public static Map doXMLParse(String strxml) throws JDOMException, IOException {
      strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");

      if(null == strxml || "".equals(strxml)) {
         return null;
      }
      
      Map m = new HashMap();
      
      InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
      SAXBuilder builder = new SAXBuilder();
      Document doc = builder.build(in);
      Element root = doc.getRootElement();
      List list = root.getChildren();
      Iterator it = list.iterator();
      while(it.hasNext()) {
         Element e = (Element) it.next();
         String k = e.getName();
         String v = "";
         List children = e.getChildren();
         if(children.isEmpty()) {
            v = e.getTextNormalize();
         } else {
            v = XMLUtil.getChildrenText(children);
         }
         
         m.put(k, v);
      }
      
      //关闭流
      in.close();
      
      return m;
   }
   /**
    * @Author: HONGLINCHEN
    * @Description: 微信支付 解析xml xml转map  获取prepay_id
    * @param xml
    * @Date: 2017-9-8 10:13
    */
   public static Map<String,Object> getResult(String xml){
      Map<String,Object> map = new HashMap<String, Object>();
      try {
         org.dom4j.Document document = DocumentHelper.parseText(xml);
         org.dom4j.Element root = document.getRootElement();
         Iterator<org.dom4j.Element> it = root.elementIterator();
         while (it.hasNext()) {
            org.dom4j.Element element = it.next();
            map.put(element.getName(), element.getTextTrim());
         }
      } catch (DocumentException e) {
         e.printStackTrace();
      }
      return map;
   }
   /**
    * 获取子结点的xml
    * @param children
    * @return String
    */
   public static String getChildrenText(List children) {
      StringBuffer sb = new StringBuffer();
      if(!children.isEmpty()) {
         Iterator it = children.iterator();
         while(it.hasNext()) {
            Element e = (Element) it.next();
            String name = e.getName();
            String value = e.getTextNormalize();
            List list = e.getChildren();
            sb.append("<" + name + ">");
            if(!list.isEmpty()) {
               sb.append(XMLUtil.getChildrenText(list));
            }
            sb.append(value);
            sb.append("</" + name + ">");
         }
      }
      
      return sb.toString();
   }
   
   /**
    * 获取xml编码字符集
    * @param strxml
    * @return
    * @throws IOException 
    * @throws JDOMException 
    */
   public static String getXMLEncoding(String strxml) throws JDOMException, IOException {
      InputStream in = String2Inputstream(strxml);
      SAXBuilder builder = new SAXBuilder();
      Document doc = builder.build(in);
      in.close();
      return (String)doc.getProperty("encoding");
   }
   public static InputStream String2Inputstream(String str) {
      return new ByteArrayInputStream(str.getBytes());
   }

   // 请求xml组装
   public static String getRequestXml(SortedMap<String, Object> parameters) {
      StringBuffer sb = new StringBuffer();
      sb.append("<xml>");
      Set es = parameters.entrySet();

      Iterator it = es.iterator();
      while (it.hasNext()) {
         Map.Entry entry = (Map.Entry) it.next();
         String key = (String) entry.getKey();
         // String value = (String) entry.getValue();
         Object value = entry.getValue();
         // || "body".equalsIgnoreCase(key) || "sign".equalsIgnoreCase(key)
         if ("attach".equalsIgnoreCase(key) || "body".equalsIgnoreCase(key) || "sign".equalsIgnoreCase(key)) {
            sb.append("<" + key + ">" + "<![CDATA[" + value + "]]></" + key + ">");
         } else {
            sb.append("<" + key + ">" + value + "</" + key + ">");
         }
      }
      sb.append("</xml>");
      return sb.toString();
   }
   
}
